﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;

using EFong.ServicesInterface;
using EFong.Contract;
using UIShare;

namespace EFong.View.Proxy
{
    public class MasterServiceProxy : ClientBase<IMasterService>, IMasterService
    {
        #region Constructor

        public MasterServiceProxy(string endpointConfigurationName) : 
            base(endpointConfigurationName)
        { 
        }

        public MasterServiceProxy(InstanceContext callbackInstance) :
            base(callbackInstance)
        {
        }

        public MasterServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName) :
            base(callbackInstance, endpointConfigurationName)
        {
        }

        public MasterServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, string remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public MasterServiceProxy(InstanceContext callbackInstance, string endpointConfigurationName, EndpointAddress remoteAddress) :
            base(callbackInstance, endpointConfigurationName, remoteAddress)
        {
        }

        public MasterServiceProxy(InstanceContext callbackInstance, Binding binding, EndpointAddress remoteAddress) :
            base(callbackInstance, binding, remoteAddress)
        {
        }

        public MasterServiceProxy(Binding binding, EndpointAddress remoteAddress) :
            base( binding, remoteAddress)
        {
        }

        private static MasterServiceProxy _instance;
        public static MasterServiceProxy GetInstance() 
        {
            if (_instance == null || _instance.State == CommunicationState.Faulted)
            {
                string url = string.Format("Http://{0}:{1}/{2}", ProxyMgt.IP, ProxyMgt.Port, "MasterService");

                EndpointAddress epa = new EndpointAddress(url);

                WSHttpBinding binding = new WSHttpBinding();
                //binding.ReliableSession.Enabled = true;
                binding.Security.Mode = SecurityMode.None;
                binding.SendTimeout = new TimeSpan(0, 1, 30);
                binding.MaxReceivedMessageSize = 65536000;
                binding.ReaderQuotas.MaxArrayLength = 65536000;
                binding.ReaderQuotas.MaxStringContentLength = 65536000;

                _instance = new MasterServiceProxy(binding, epa);
            }
            return _instance;
        }

        #endregion

        #region IMasterService 成员

        public ServiceResult<List<Contract.Project>> GetAllProject()
        {
            var result = base.Channel.GetAllProject();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.Project>> GetAllOtherProject()
        {
            var result = base.Channel.GetAllOtherProject();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult NewProject(Contract.Project project)
        {
            var result = base.Channel.NewProject(project);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateProject(Contract.Project project)
        {
            var result = base.Channel.UpdateProject(project);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateProjectList(List<Contract.Project> projectList)
        {
            var result = base.Channel.UpdateProjectList(projectList);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }


        public ServiceResult<List<Contract.BankAccount>> GetAllBankAccount()
        {
            var result = base.Channel.GetAllBankAccount();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult NewBankAccount(Contract.BankAccount bankAccount)
        {
            var result = base.Channel.NewBankAccount(bankAccount);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateBankAccount(Contract.BankAccount bankAccount)
        {
            var result = base.Channel.UpdateBankAccount(bankAccount);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateBankAccounts(List<Contract.BankAccount> bankAccounts)
        {
            var result = base.Channel.UpdateBankAccounts(bankAccounts);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RCustomer>> GetAllCustomer()
        {
            var result = base.Channel.GetAllCustomer();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RCustomer>> GetCustomerByName(string name)
        {
            var result = base.Channel.GetCustomerByName(name);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RCustomer>> GetCustomerByAddress(string address)
        {
            var result = base.Channel.GetCustomerByAddress(address);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RCustomer>> GetCustomerByTel(string tel)
        {
            var result = base.Channel.GetCustomerByTel(tel);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RCustomer>> GetCustomerBySalesman(User salesman)
        {
            var result = base.Channel.GetCustomerBySalesman(salesman);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<Contract.Customer> GetCustomerByID(Guid id) 
        {
            var result = base.Channel.GetCustomerByID(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> NewCustomer(Contract.Customer customer)
        {
            var result = base.Channel.NewCustomer(customer);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateCustomer(Contract.Customer customer)
        {
            var result = base.Channel.UpdateCustomer(customer);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }
        
        public ServiceResult DeleteCustomer(Contract.Customer customer)
        {
            var result = base.Channel.DeleteCustomer(customer);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RVendor>> GetAllVendor()
        {
            var result = base.Channel.GetAllVendor();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RVendor>> GetAllHotelVendor()
        {
            var result = base.Channel.GetAllHotelVendor();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RVendor>> GetAllTrainVendor()
        {
            var result = base.Channel.GetAllTrainVendor();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RVendor>> GetAllFlightVendor()
        {
            var result = base.Channel.GetAllFlightVendor();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RVendor>> GetAllInsuranceVendor()
        {
            var result = base.Channel.GetAllInsuranceVendor();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<Contract.Vendor> GetVendorByID(Guid id) 
        {
            var result = base.Channel.GetVendorByID(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<Contract.RVendor>> GetVendorByProject(Contract.Project project) 
        {
            var result = base.Channel.GetVendorByProject(project);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<string> NewVendor(Contract.Vendor vendor)
        {
            var result = base.Channel.NewVendor(vendor);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateVendor(Contract.Vendor vendor)
        {
            var result = base.Channel.UpdateVendor(vendor);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<InsurancePolicy>> GetAllInsurancePolicy()
        {
            var result = base.Channel.GetAllInsurancePolicy();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<InsurancePolicy>> GetAllAvailableInsurancePolicy()
        {
            var result = base.Channel.GetAllAvailableInsurancePolicy();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult NewInsurancePolicy(Contract.NewInsurancePolicy newInsurancePolicy)
        {
            var result = base.Channel.NewInsurancePolicy(newInsurancePolicy);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateInsurancePolicy(InsurancePolicy insurancePolicy)
        {
            var result = base.Channel.UpdateInsurancePolicy(insurancePolicy);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<InsurancePolicy> GetAvailableInsurancePolicy(Contract.RVendor vendor, string type)
        {
            var result = base.Channel.GetAvailableInsurancePolicy(vendor,type);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<Contract.InsurancePolicy> ReturnInsurancePolicy(Contract.InsurancePolicy oldInsurancePolicy , string newNr) 
        {
            var result = base.Channel.ReturnInsurancePolicy(oldInsurancePolicy,newNr);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<string>> GetInsurancePolicyTypeList()
        {
            var result = base.Channel.GetInsurancePolicyTypeList();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<JourneyOrder>> GetAllJourneyOrder()
        {
            var result = base.Channel.GetAllJourneyOrder();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<JourneyOrder>> GetAllAvailableJourneyOrder()
        {
            var result = base.Channel.GetAllAvailableJourneyOrder();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult NewJourneyOrder(List<JourneyOrder> journeyOrderList)
        {
            var result = base.Channel.NewJourneyOrder(journeyOrderList);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<Contract.JourneyOrder> ReturnJourneyOrder(Contract.JourneyOrder oldJourneyOrder, string newNr) 
        {
            var result = base.Channel.ReturnJourneyOrder(oldJourneyOrder, newNr);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult UpdateJourneyOrder(JourneyOrder journeyOrder)
        {
            var result = base.Channel.UpdateJourneyOrder(journeyOrder);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<JourneyOrder> GetAvailableJourneyOrder(string type)
        {
            var result = base.Channel.GetAvailableJourneyOrder(type);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public ServiceResult<List<string>> GetJourneyOrderTypeList()
        {
            var result = base.Channel.GetJourneyOrderTypeList();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult UpdateTourVendor(Contract.TourVendor tourVendor)
        {
            var result = base.Channel.UpdateTourVendor(tourVendor);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.RTourVendor>> GetAllTourVendor()
        {
            var result = base.Channel.GetAllTourVendor();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.RVendor>> GetAllTourpartyVendor()
        {
            var result = base.Channel.GetAllTourpartyVendor();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<Contract.TourVendor> GetTourVendorByID(Guid id)
        {
            var result = base.Channel.GetTourVendorByID(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.SearchTour>> GetTourByName(string name)
        {
            var result = base.Channel.GetTourByName(name);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<string> UpdateTouringParty(Contract.TouringParty touringParty)
        {
            var result = base.Channel.UpdateTouringParty(touringParty);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.RTouringParty>> GetAllTouringParty()
        {
            var result = base.Channel.GetAllTouringParty();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<Contract.TouringParty> GetTouringPartyByID(Guid id)
        {
            var result = base.Channel.GetTouringPartyByID(id);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult VoidTouringParty(Contract.TouringParty touringParty, string cause) 
        {
            var result = base.Channel.VoidTouringParty(touringParty,cause);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.STour>> SearchTourByNameAndTravel(string name, string vendor, string type, string area, string feature, string travel)
        {
            var result = base.Channel.SearchTourByNameAndTravel(name, vendor,type,area,feature,travel);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult<List<Contract.TourItemType>> GetTourItemTypeList()
        {
            var result = base.Channel.GetTourItemTypeList();
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        public Contract.ServiceResult UpdateTourItemTypeList(List<Contract.TourItemType> list)
        {
            var result = base.Channel.UpdateTourItemTypeList(list);
            if (!result.IsSuccess)
                MsgBox.Error(result.FailMessage);
            return result;
        }

        #endregion
         
    }
}
